#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define	MAX_NUMBER	110
#define PSIZE 101

//typedef unsigned long long u64;
typedef unsigned int u64;
typedef unsigned int u32;

typedef struct File {
	int nr;
	int r;
	int q;
	int t;
} file_t;

typedef struct Prio {
	file_t files[PSIZE];
	int size;
	int ptr;
	int q_sum;
	int t_sum;
	int m;
} prio_t;

int cmp (const void * a, const void * b)
{
  return ((file_t*)a)->t > ((file_t*)b)->t;
}
//=================================================================
//prio methods

void prio_print(prio_t * prio) {
	int i, j;
	printf("[q_sum=%d,ptr=%d,size=%d]\n", prio->q_sum, prio->ptr, prio->size);
	printf("nr\tr\tq\tt\n");
	for(j = 0; j < prio->size; j++) {
		printf("%02d\t%d\t%d\t%d\n", prio->files[j].nr, prio->files[j].r, prio->files[j].q, prio->files[j].t);
	}
	printf("\n");
}
//-----------------------------------------------------------------
void prio_init(prio_t * prio) {
	int i, j;
	prio->size = 0;
	prio->q_sum = 0;
	prio->t_sum = 0;
	prio->ptr = 0;
	prio->m = 0;
	for(j = 0; j < PSIZE; j++) {
		prio->files[j].nr = 0;
		prio->files[j].r = 0;
		prio->files[j].q = 0;
		prio->files[j].t = 0;
	}
}
//-----------------------------------------------------------------
void prio_file_insert(prio_t * prio, file_t * f) {
	file_t * file_dest = &(prio->files[prio->size++]);
	memcpy(file_dest, f, sizeof(file_t));
	prio->q_sum += f->q;
}
//-----------------------------------------------------------------
int get_time(int r, int q_sum, int m, int q) {
	return (u32)((u64)(r * q_sum) / (u64)(m * q));
}
//-----------------------------------------------------------------
int get_size(int t, int q_sum, int m, int q) {
	return (u32)((u64)(m * q * t) / (u64)q_sum);
}
//-----------------------------------------------------------------
void file_set_time(file_t * file, int m, int q_sum) {
	file->t = get_time(file->r, q_sum, m, file->q);
}
//-----------------------------------------------------------------
void prio_run(prio_t * prio) {
	int i_min, i, t, j;
	u32 t_min;
	file_t *fmin;
	
	i = 0;
	//calculate sume and time foreach
	for(i = 0; i < prio->size; i++) {
		file_set_time(&prio->files[i], prio->m, prio->q_sum);
	}
	
	//sort by time
	qsort(prio->files, prio->size, sizeof(file_t), cmp);

	for(i = 1; i < prio->size; i++) {
		for(j = i; j < prio->size; j++) {
			int size = get_size(prio->files[i-1].t, prio->q_sum, prio->m, prio->files[j].q);
			prio->files[j].r -= size;
		}
		prio->q_sum -= prio->files[i - 1].q;
		//update time
		file_set_time(&prio->files[i], prio->m, prio->q_sum);
	}
}
//=================================================================

int main() {

	int n, m, nr, p, q, r, i, j, s = 0;;
	file_t f;
	prio_t tab_prio[MAX_NUMBER];
	
	for(i = 0; i < MAX_NUMBER; i++)
		prio_init(&tab_prio[i]);

	scanf("%d %d", &n, &m);

	for(i = 0; i < n; i++) {
		scanf("%d %d %d %d", &(f.nr), &p, &(f.q), &(f.r));
		f.t = 0.0;
		prio_file_insert(&tab_prio[p], &f);
		tab_prio[p].m = m;
	}

	for(i = 0; i < MAX_NUMBER; i++) {
		if(tab_prio[i].size > 0) {
			prio_run(&tab_prio[i]);
		}
	}
	
	for(i = 0; i < MAX_NUMBER; i++) {
		for(j = 0; j < tab_prio[i].size; j++) {
			s += tab_prio[i].files[j].t;
			printf("%d %d\n", tab_prio[i].files[j].nr, s);
		}
	}
	
	return 0;
}

